package whitefox.gqtcard;

import java.util.ArrayList;

import whitefox.gqtcard.dblocation.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends Activity implements LocationListener {

	private final LatLng LOCATION_HoChiMinh = new LatLng(10.823864, 106.635994);
	private final LatLng LOCATION_HaiPhong = new LatLng(20.863624, 106.680626);

	String[] items = { "Sai gon", "Ha noi" };
	LatLng LOCATION_MY = null;
	LatLng LOCATION_DB = null;
	GetLocations mylocationAddress;
	GoogleMap map;
	private LocationManager locationManager;
	private String provider;
	Spinner Scity;
	double LatLng_X = 0, LatLng_Y = 0;
	ArrayList<whitefox.gqtcard.dblocation.Location> ListLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		// khoi tao map
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		map.setMyLocationEnabled(true);
		// khoi tao Data base
		final DBLocation dbl = new DBLocation(this);
		ListLocation = new ArrayList<whitefox.gqtcard.dblocation.Location>();
		ListLocation = dbl.findall();
		// su kien click dia diem tren map
		map.setOnMapClickListener(new OnMapClickListener() {

			@Override
			public void onMapClick(LatLng point) {
				// map.addMarker(new MarkerOptions().position(point));
				Toast.makeText(getApplicationContext(),
						point.latitude + " : " + point.longitude,
						Toast.LENGTH_LONG).show();
				LatLng_X = point.latitude;
				LatLng_Y = point.longitude;
			}
		});

		drawMarker(ListLocation);
		
		// su kien add dia diem
		Button btnAdd = (Button) findViewById(R.id.buttonAdd);
		btnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MapActivity.this, SetLocations.class);
				intent.putExtra("MyLatitude", LatLng_X);
				intent.putExtra("MyLongitude", LatLng_Y);
				startActivity(intent);
			}
		});

		// khoi dong ban tim kiem
		Button btnCheck = (Button) findViewById(R.id.buttonCheck);
		btnCheck.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MapActivity.this,
						CheckLocation.class);
				startActivity(intent);
			}
		});

		// ban danh sach thanh pho
		Scity = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<String> city = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, items);
		city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Scity.setAdapter(city);

		// su kiem chon thanh pho diem den
		// map bi loi khi them code doan nay chua tim hieu dc nguyen nhan
		/*
		 * Scity.setOnItemClickListener(new OnItemClickListener() {
		 * 
		 * @Override public void onItemClick(AdapterView<?> arg0, View arg1, int
		 * arg2, long arg3) { if (arg2 ==0){
		 * 
		 * CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
		 * LOCATION_HoChiMinh, 9); map.animateCamera(update);
		 * 
		 * }else{
		 * 
		 * CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
		 * LOCATION_HaiPhong, 9); map.animateCamera(update); } } });
		 */

		// hien thi thong tin dia diem
		map.setOnMarkerClickListener(new OnMarkerClickListener() {
			
			@Override
			public boolean onMarkerClick(Marker marker) {
				for ( final whitefox.gqtcard.dblocation.Location x : ListLocation){
					if (x.getM_latitude()== (marker.getPosition().latitude)){
						if (x.getM_longitude() == marker.getPosition().longitude){
						///// su dung sau
						}
					}
						
					
				}
				return false;
			}
		});
		
		// danh dau dia diem GPS
		mylocationAddress = new GetLocations(getApplicationContext());
		LOCATION_MY = new LatLng(mylocationAddress.getLatitude(),
				mylocationAddress.getLongitude());

		CameraUpdate mainCamera = CameraUpdateFactory.newLatLngZoom(
				LOCATION_MY, 16);
		map.animateCamera(mainCamera);
		map.addMarker(new MarkerOptions()
				.icon(BitmapDescriptorFactory
						.fromResource(R.drawable.ic_launcher))
				.anchor(0.0f, 1.0f) // Anchors
									// the
									// marker
									// on
									// the
									// bottom
									// left
				.position(LOCATION_MY));

		// set GPS service with android.location Libraries
		LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
		boolean enabledGPS = service
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		boolean enabledWiFi = service
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

		// Check if enabled and if not send user to the GSP settings
		// Better solution would be to display a dialog and suggesting to
		// go to the settings
		if (!enabledGPS) {
			Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG)
					.show();
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivity(intent);
		}

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		// Define the criteria how to select the locatioin provider -> use
		// default
		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);

		// Initialize the location fields
		if (location != null) {
			Toast.makeText(this, "Selected Provider " + provider,
					Toast.LENGTH_SHORT).show();
			onLocationChanged(location);
		} else {

			// do something
		}

	}

	// ve lai cac dia diem can thiet
	private void drawMarker(
			ArrayList<whitefox.gqtcard.dblocation.Location> location) {
		for ( whitefox.gqtcard.dblocation.Location x : location) {
			LOCATION_DB = new LatLng(x.m_latitude, x.m_longitude);
			map.addMarker(new MarkerOptions().position(LOCATION_DB).title(x.getM_Name_Location()));
//			map.setOnMarkerClickListener(new OnMarkerClickListener() {
//				
//				@Override
//				public boolean onMarkerClick(Marker marker) {
//					marker.setTitle(x.getM_Name_Location());
//					return false;
//				}
//			});
		}

	}

	// code tu phat sinh chua su ly
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
